﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BlueCMS.Core.Data;
using BlueCMS.Core.Data.Query;
using BlueCMS.Core.Entities;

namespace BlueCMS.Core.Services
{
    public class TemplateService
    {
        public static Template GetTemplateForId(Guid id)
        {
            Template template;

            SelectQuery query = new SelectQuery();
            query.Select.All();
            query.From.Table("cms_template_tbl");
            query.Where
                .Column("template_id").Equals().Parameter(id)
                .And()
                .Column("isactive").Equals().Constant(true)
                .And()
                .Column("isdeleted").Equals().Constant(false);

            template = query.ExecuteObject<Template>();

            if (template != null)
            {
                query = new SelectQuery();
                query.Select.All();
                query.From.Table("cms_templatefragment_tbl");
                query.Where.Column("template_id").Equals().Parameter(id);
                query.OrderBy.Column("displayorder");

                template.Fragments = query.ExecuteCollection<TemplateFragment>();
            }

            return template;
        }
    }
}
